Test Failed
Push — main ( a6b018...b2d6b6 )
by Ehsan
02:40
created

TweetUser.isPublic   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
1 3
import moment from 'moment';
2
import {Twitter} from 'twit';
3 3
import Constant from './Constant';
4 3
import Helper from './Helper';
5 3
import {ReTweetableAbstract} from './ReTweetable';
6
7
export type TweetUserConfig = {
8
    minCreationDiff: number,
9
    minFollowers: number,
10
    minTweets: number,
11
    userBlocklist?: string[]
12
};
13
14 3
export default class TweetUser extends ReTweetableAbstract {
15
    rawUser: Twitter.User;
16
    config: TweetUserConfig;
17
18
    constructor(rawUser: Twitter.User, config: TweetUserConfig) {
19 104
        super();
20 104
        this.rawUser = rawUser;
21 104
        this.config = config;
22
    }
23
24
    isPublic(): boolean {
25 29
        return !this.rawUser.protected;
26
    }
27
28
    isCreatedRecently(): boolean {
29 24
        const createdAt = moment(this.rawUser.created_at, Constant.TWITTER_DATETIME_FORMAT, Constant.LANG_EN);
30 24
        const now = moment();
31
32 24
        return now.diff(createdAt, Constant.MOMENT_DAYS) < this.config.minCreationDiff;
33
    }
34
35
    hasEnoughFollowers(): boolean {
36 21
        return this.rawUser.followers_count >= this.config.minFollowers;
37
    }
38
39
    hasEnoughTweets(): boolean {
40 18
        return this.rawUser.statuses_count >= this.config.minTweets;
41
    }
42
43
    isBlockListed(): boolean {
44 32
        return Helper.objectExists(this.config.userBlocklist) && this.config.userBlocklist.includes(this.rawUser.id_str);
45
    }
46
47
    isReTweetable(): boolean {
48 26
        if (this.isBlockListed()) {
49 3
            this.retweetError = 'User is in blocklist';
50 3
            return false;
51
        }
52
53 23
        if (!this.isPublic()) {
54 5
            this.retweetError = 'User is not public';
55 5
            return false;
56
        }
57
58 18
        if (this.isCreatedRecently()) {
59 3
            this.retweetError = 'User is created recently';
60 3
            return false;
61
        }
62
63 15
        if (!this.hasEnoughFollowers()) {
64 3
            this.retweetError = 'User does not have enough followers';
65 3
            return false;
66
        }
67
68 12
        if (!this.hasEnoughTweets()) {
69 3
            this.retweetError = 'User does not have enough tweets';
70 3
            return false;
71
        }
72
73 9
        return true;
74
    }
75
}